package com.lihuan.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lihuan.beans.*;
import com.lihuan.common.util.ListToTree;
import com.lihuan.common.util.UserUtils;
import com.lihuan.dao.MessageDao;
import com.lihuan.entity.Message;
import com.lihuan.entity.User;
import com.lihuan.service.MessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    MessageDao messageDao;

    /** 查询留言列表 */
    @Override
    public PageInfo<MessageListItem> listMessage(MessageList param) {
        PageHelper.startPage(param.getPageIndex(), param.getPageSize());
        return new PageInfo<MessageListItem>(messageDao.listMessage(param));
    }

    /** 查询个人留言列表 */
    @Override
    public PageInfo<MessageListItem> listPersonMessage(MessageList param) {
        User currentUser = UserUtils.getCurrentUser();
        param.setUserId(currentUser.getId());
        PageHelper.startPage(param.getPageIndex(), param.getPageSize());
        return new PageInfo<MessageListItem>(messageDao.listMessage(param));
    }

    /** 添加留言 */
    @Override
    public void addMessage(MessageAdd param) {
        User currentUser = UserUtils.getCurrentUser();
        Assert.isTrue(null != currentUser,
                "请先登录");
        Date date = new Date();
        Message bean = new Message();
        BeanUtils.copyProperties(param,bean);
        bean.setCreateTime(date);
        bean.setUserId(currentUser.getId());
        messageDao.addMessage(bean);
    }

    /** 修改留言 */
    @Override
    public void editMessage(MessageEdit param) {
        Assert.isTrue(null != UserUtils.getCurrentUser(),
                "请先登录");
        Message bean = this.existMessageById(param.getId());
        BeanUtils.copyProperties(param,bean);
        messageDao.editMessage(bean);
    }

    /** 删除留言 */
    @Override
    public void removeMessage(MessageId param) {
        Assert.isTrue(null != UserUtils.getCurrentUser(),
                "请先登录");
        //检查对象是否存在
        this.existMessageById(param.getId());
        messageDao.removeMessage(param);
    }

    /** 留言详情 */
    @Override
    public MessageInfo infoMessage(MessageId param) {
        //检查对象是否存在
        this.existMessageById(param.getId());
        MessageInfo messageInfo = messageDao.infoMessage(param.getId());
        MessageInfo info = new MessageInfo();
        BeanUtils.copyProperties(messageInfo,info);
        return info;
    }

    /** 根据 ID 获取 留言 对象 */
    @Override
    public Message getMessageById(String id) {
        return messageDao.getMessageById(id);
    }

    @Override
    public  JSONArray listToTree() {
        List<Tree> data = new ArrayList<>();
        Tree tree1 = new Tree();
        tree1.setId(1);
        tree1.setPid(0);
        tree1.setName("甘肃省");
        Tree tree2 = new Tree();
        tree2.setId(2);
        tree2.setPid(1);
        tree2.setName("天水市");
        Tree tree3 = new Tree();
        tree3.setId(3);
        tree3.setPid(2);
        tree3.setName("秦州区");
        Tree tree4 = new Tree();
        tree4.setId(4);
        tree4.setPid(0);
        tree4.setName("北京市");
        Tree tree5 = new Tree();
        tree5.setId(5);
        tree5.setPid(4);
        tree5.setName("昌平区");
        data.add(tree1);
        data.add(tree2);
        data.add(tree3);
        data.add(tree4);
        data.add(tree5);
       return ListToTree.listToTree(JSONArray.parseArray(JSON.toJSONString(data)),"id","pid","children");
    }
}
